package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.common.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.sercive.CheckGroupService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController{
    @Reference
    CheckGroupService checkGroupService;
    @RequestMapping("/add")
    //请求体的数据需要@RequestBody转换json字符串
    public Result add(@RequestBody Map<String,Object> map) {
        log.debug("map: {"+map.toString() +"}");

        JSONArray jsonArray = (JSONArray)map.get("checkitemIds");
        Integer[] checkItemIds = jsonArray.toArray(new Integer[]{});

        log.debug("checkItemIds:" + Arrays.toString(checkItemIds));

        JSONObject jsonObject = (JSONObject)map.get("checkGroup");
        CheckGroup checkGroup = jsonObject.toJavaObject(CheckGroup.class);
        log.debug("checkGroup:" +checkGroup.getCode());
        checkGroupService.add(checkGroup,checkItemIds);
        //log.debug("checkGroup:" +checkGroup);
        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, pageResult);
    }

    @RequestMapping("/findById")
    public Result findById(int id) {
        CheckGroup checkGroup=checkGroupService.findById(id);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(int id) {
        List<Integer> list = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> map) {
        JSONObject jsonObject = (JSONObject) map.get("checkGroup");
        CheckGroup checkGroup = jsonObject.toJavaObject(CheckGroup.class);
        JSONArray jsonArray = (JSONArray) map.get("checkitemIds");
        Integer[] checkitemIds = jsonArray.toArray(new Integer[]{});
        checkGroupService.edit(checkGroup,checkitemIds);
        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/delById")
    public Result delById(int id) {
        checkGroupService.delById(id);
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }
}
